package org.gdufe.users.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.gdufe.pojos.dto.InsertUserDto;
import org.gdufe.pojos.dto.PageParams;
import org.gdufe.pojos.dto.UpdateUserDto;
import org.gdufe.pojos.pojo.User;
import org.gdufe.pojos.vo.PageResponseResult;
import org.gdufe.pojos.vo.R;
import org.gdufe.users.mapper.UserMapper;
import org.gdufe.users.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResponseResult querry(PageParams pageParams, String username) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if (username!=null&&!username.isEmpty()){
            wrapper.like("username",username);
        }
        Long pageNo = pageParams.getPageNo();
        Long pageSize = pageParams.getPageSize();
        if (pageNo<=0){
            pageNo=1L;
        }
        if (pageSize<=0){
            pageSize=10L;
        }
        IPage<User> page=new Page<>(pageNo, pageSize);
        IPage<User> userIPage = userMapper.selectPage(page, wrapper);
        PageResponseResult responseResult=new PageResponseResult((int) userIPage.getCurrent(), (int) userIPage.getSize(), (int) userIPage.getTotal());
        List<User> records = userIPage.getRecords();
        responseResult.setData(records);
        return responseResult;
    }

    @Override
    public R select(Integer id) {
        User user = userMapper.selectById(id);
        if (user==null){
            return R.errorResult(500,"查无此人");
        }
        return R.okResult(user);
    }


    @Override
    public R insert(InsertUserDto dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        if (username==null||username.isEmpty()||password==null||password.isEmpty()){
            return R.errorResult(500,"参数错误，请输入格式正确的用户名和密码");
        }
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        String[] str={"normal"};
        String json = JSON.toJSONString(str);
        user.setAuthorities(json);
        System.out.println(json);
        int i = userMapper.insert(user);
        if (i<=0){
            return R.errorResult(500,"插入失败");
        }
        return R.okResult(200,"插入成功");
    }

    @Override
    public R update(UpdateUserDto dto) {
        String username = dto.getUsername();
        String password = dto.getPassword();
        String authorities = dto.getAuthorities();
        if (username==null||username.isEmpty()||password==null||password.isEmpty()||authorities==null||authorities.isEmpty()){
            return R.errorResult(500,"参数错误");
        }
        User user=new User();
        user.setPassword(password);
        user.setUsername(username);
        user.setAuthorities(authorities);
        user.setId(dto.getId());

        int i = userMapper.updateById(user);
        if (i<=0){
            return R.errorResult(500,"更新失败");
        }
        return R.okResult(200,"更新成功");
    }

    @Override
    public R delete(Integer id) {
        int i = userMapper.deleteById(id);
        if (i<=0){
            return R.errorResult(500,"删除失败");
        }
        return R.okResult(200,"删除成功");
    }

    @Override
    public R selectByUsername(String username) {
        if (username==null||username.isEmpty()){
            return R.errorResult(500,"username格式错误");
        }
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        if (user==null){
            return R.errorResult(500,"查无此人");
        }
        return R.okResult(user);
    }
}
